package com.ruoyi.mes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.mes.domain.*;
import com.ruoyi.mes.service.impl.WkspMaterialListDetailServiceImpl;
import com.ruoyi.mes.service.IWkspMaterialListDetailService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.service.ILnyStorageOutService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出库管理Controller
 * 
 * @author CFL
 * @date 2024-04-28
 */
@RestController
@RequestMapping("/mes/retrieval")
public class LnyStorageOutController extends BaseController
{
    @Autowired
    private ILnyStorageOutService lnyStorageOutService;

    @Autowired
    private IWkspMaterialListDetailService wkspMaterialListDetailService;
    /**
     * 查询出库管理列表
     */
    @PreAuthorize("@ss.hasPermi('mes:retrieval:list')")
    @GetMapping("/list")
    public TableDataInfo list(LnyStorageOut lnyStorageOut)
    {
        startPage();
        List<LnyStorageOut> list = lnyStorageOutService.selectLnyStorageOutList(lnyStorageOut);
        return getDataTable(list);
    }

    /**
     * 导出出库管理列表
     */
    @PreAuthorize("@ss.hasPermi('mes:retrieval:export')")
    @Log(title = "出库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LnyStorageOut lnyStorageOut)
    {
        List<LnyStorageOut> list = lnyStorageOutService.selectLnyStorageOutList(lnyStorageOut);
        ExcelUtil<LnyStorageOut> util = new ExcelUtil<LnyStorageOut>(LnyStorageOut.class);
        util.exportExcel(response, list, "出库管理数据");
    }

    /**
     * 获取出库管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:retrieval:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(lnyStorageOutService.selectLnyStorageOutById(id));
    }

    /**
     * 新增出库管理
     */
    @PreAuthorize("@ss.hasPermi('mes:retrieval:add')")
    @Log(title = "出库管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LnyStorageOut lnyStorageOut)
    {
        return toAjax(lnyStorageOutService.insertLnyStorageOut(lnyStorageOut));
    }

    /**
     * 销售出库新增出库管理
     */
    @PreAuthorize("@ss.hasPermi('mes:retrieval:outOrder')")
    @PostMapping("/outOrder")
    public AjaxResult addToOutOrder(@RequestBody List<LnyOutOrder> lnyOutOrder)
    {
        return toAjax(lnyStorageOutService.insertLnyOutOrder(lnyOutOrder));
    }

    /**
     * 物料出库的出库按钮调用查询接口
     */
    @PreAuthorize("@ss.hasPermi('mes:retrieval:material:check:query')")
    @GetMapping(value = "/material/check/{mldId}")
    public TableDataInfo materialList(@PathVariable("mldId") Long mldId)
    {
        List<WkspMaterialListDetail> list = wkspMaterialListDetailService.selectFromMaterialOut(mldId);
        return getDataTable(list);
    }

    /**
     * 销售出库的出库按钮调用查询接口
     */
    @PreAuthorize("@ss.hasPermi('mes:retrieval:order:check:query')")
    @GetMapping(value = "/order/check/{id}")
    public TableDataInfo productList(@PathVariable("id") Long id)
    {
        List<LnyOutOrder> list = lnyStorageOutService.selectFromMaterialOutByOutId(id);
        return getDataTable(list);
    }

    /**
     * 物料出库后改库存数据
     */
    @PreAuthorize("@ss.hasPermi('mes:retrieval:material')")
    @Log(title = "物料出库", businessType = BusinessType.UPDATE)
    @PutMapping("/material")
    public AjaxResult subStoreroomFromMaterial(@RequestBody List<LnyStorageOut> lnyStorageOut)
    {
        return toAjax(lnyStorageOutService.updateStoreroomFromMaterial(lnyStorageOut));
    }

    /**
     * 销售出库后改库存数据
     */
    @PreAuthorize("@ss.hasPermi('mes:retrieval:order')")
    @PutMapping("/order")
    public AjaxResult subStoreroomFromOrder(@RequestBody List<LnyOutOrder> lnyOutOrder)
    {
        return toAjax(lnyStorageOutService.updateStoreroomFromOrder(lnyOutOrder));
    }

    /**
     * 修改出库管理
     */
    @PreAuthorize("@ss.hasPermi('mes:retrieval:edit')")
    @Log(title = "出库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LnyStorageOut lnyStorageOut)
    {
        return toAjax(lnyStorageOutService.updateLnyStorageOut(lnyStorageOut));
    }

    /**
     * 删除出库管理
     */
    @PreAuthorize("@ss.hasPermi('mes:retrieval:remove')")
    @Log(title = "出库管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(lnyStorageOutService.deleteLnyStorageOutByIds(ids));
    }
}
